package com.djmw.mouse.service;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

public class ServiceUtil {
    /**
     * 创建分页请求.
     * 
     * @param pageNumber
     * @param pagzSize
     * @param sortType
     *            sort field name or "auto" to use default value.
     * @param sortDir
     *            a: ASC, d: DESC
     * @param defaultSortDirStr
     *            string of "type:dir", type is the name, dir is "d" for DESC or
     *            "a" for ASC
     * @return
     */
    public static PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType, String sortDir,
            String defaultSortDirStr) {
        if ("auto".equals(sortType)) {
            String[] defaultSortDir = defaultSortDirStr.split(":");
            sortType = defaultSortDir[0];
            sortDir = defaultSortDir[1];
        }
        Sort sort = new Sort("d".equalsIgnoreCase(sortDir) ? Direction.DESC : Direction.ASC, sortType);
        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

}
